package com.example.photongrid;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.net.ServerSocket;
import java.net.Socket;

import android.util.Log;


public class ServerListeningThread implements Runnable {

	ObjectInputStream in;
	ObjectOutputStream out;
	Socket socket;
	int playerno;
	ServerSocket serverSocket;
	public static boolean update;
	int c=0;
	Boolean updateWalls = false;
	//private Board board;
	ServerSendingThread ssd=null;
	ServerListeningThread(Socket socket, ServerSocket serverSocket, int player)
	{
		//this.in = input;
		in = null;
		out = null;
		playerno = player;
		this.socket = socket;
		this.serverSocket = serverSocket;
		//this.board = board;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("Server Listening Thread Started");
		Log.i("Server", "Server Listening Thread Started");
		
		while(true)
		{
			if(out == null){
				System.out.println("Outstream found NULL");
				try {
					out = new ObjectOutputStream(socket.getOutputStream());
					out.flush();
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(in == null){
				System.out.println("Instream found NULL");
				try {
					in = new ObjectInputStream(socket.getInputStream());
					System.out.println("Input Stream attached");
				} catch (StreamCorruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ssd == null){
				MessageServerToClient m = new MessageServerToClient(playerno);
				try {
					
					out.writeObject(m);
					out.flush();
					Log.i("server", "Player no sent "+playerno);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ssd = new ServerSendingThread(socket, in, out);
				Thread sendingthread = new Thread(ssd);
				sendingthread.start();
			}
			 Message message = null;
			 try {
				message = (Message)in.readObject();
			} catch (OptionalDataException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(message != null){
					if(message.getMessage() == 1){
					System.out.println("Message read to be type 1");
					StateManagerServer.board.vehicleList.get(playerno).changeDirection(StateManagerServer.board.vehicleList.get(playerno).getDirection()-3);
				}
				if(message.getMessage() == 2){
					System.out.println("Message read to be type 2");
					StateManagerServer.board.vehicleList.get(playerno).changeDirection(StateManagerServer.board.vehicleList.get(playerno).getDirection()+3);
				}
			}else{
				try {
					System.out.println("No Message, Tell Thread To Wait!");
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
